package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;

import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

  @Autowired
    private RedissonClient redisson;

    /*   @Autowired
     private StringRedisTemplate stringRedisTemplate;*/
 @GetMapping(value = {"/","index.html"})
 private String indexPage(Model model) {

     //1、查出所有的一级分类
     List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
     model.addAttribute("categories",categoryEntities);

     return "index";
 }
    //index/json/catalog.json
    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();

        return catalogJson;

    }

    @ResponseBody
    @GetMapping("hello")
    public String hello(){
        RLock lock = redisson.getLock("ce-lock");
        try {
            lock.lock();
            System.out.println("加锁成功"+Thread.currentThread().getId());
            Thread.sleep(10000);
        }catch (Exception e){
            System.out.println("发生错误"+e.getMessage());
        }finally {
            System.out.println("解锁成功");
            lock.unlock();
        }
        return "hello";
    }
//读锁
     @ResponseBody
     @GetMapping("read")
    public String read(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("ReadWriteLock");
        RLock rLock = readWriteLock.readLock();
        try {
            rLock.lock();
            System.out.println("添加读锁:  "+Thread.currentThread().getId());
            Thread.sleep(10000);
        }catch (Exception e){
            System.out.println("异常"+e.getMessage());
        }finally {
            System.out.println("删除读锁:  "+Thread.currentThread().getId());
            rLock.unlock();
        }
         return "read";
    }

    //写锁
    @ResponseBody
    @GetMapping("write")
    public String write(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("ReadWriteLock");
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            System.out.println("添加写锁:  "+Thread.currentThread().getId());
            Thread.sleep(10000);
        }catch (Exception e){
            System.out.println("异常"+e.getMessage());
        }finally {
            System.out.println("删除写锁:  "+Thread.currentThread().getId());
            rLock.unlock();
        }
        return "write";
    }

    //闭锁     锁门的原理
    @ResponseBody
    @GetMapping("DownLatch")
    public String DownLatch() throws InterruptedException {
        RCountDownLatch countDownLatch = redisson.getCountDownLatch("DownLatch");
        countDownLatch.trySetCount(3);
        countDownLatch.await();
        return "放学了";
    }

    @ResponseBody
    @GetMapping("kDownLatch/{id}")
    public String kDownLatch(@PathVariable("id") Integer id) throws InterruptedException {
        RCountDownLatch latch = redisson.getCountDownLatch("DownLatch");
        latch.countDown();
        return id+"班回家";
    }

    //信号亮
    @ResponseBody
    @GetMapping("semaphoretest")
    public String semaphoretest() throws InterruptedException {
        RSemaphore semaphore = redisson.getSemaphore("semaphore");
        semaphore.acquire();   //占车位
     return "semaphoretest";
    }

    @ResponseBody
    @GetMapping("tsemaphoretest")
    public String tsemaphoretest()  {
        RSemaphore semaphore = redisson.getSemaphore("semaphore");
        semaphore.release();   //离开侧位车位
        return "semaphoretest";
    }

}
